package com.zff.rental.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 响应结果
 *
 * @author HCY
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    /**
     * 响应代码 0成功
     */
    private Integer code;

    /**
     * 响应消息/错误消息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 接口处理时间
     */
    private Long runTime;

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> of(ResultCode status, T data) {
        Result<T> result = new Result<>();
        result.setCode(status.getCode());
        result.setMsg(status.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> of(int code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> of(ResultCode status) {
        Result<T> result = new Result<>();
        result.setCode(status.getCode());
        result.setMsg(status.getMsg());
        return result;
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.CUSTOM_ERROR.getCode());
        result.setMsg(message);
        return result;
    }
}
